/***************************************************************************
 *   Copyright (C) 2008 by Tomasz Dubik                                    *
 *   tdubik@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef TASK_H
#define TASK_H

#include <string>
#include <vector>
#include <iostream>
#include <time.h>
#include "tinyxml/tinyxml.h"
 
using namespace std;
namespace Tasks
{

    typedef int UID;
    class Task
    {
        public:
            Task(Task* parent = NULL);
            Task(const Task& task);
            Task(TiXmlElement* item, Task* parent = NULL);
            ~Task();
        
            void setText(const string& newName){m_Text = newName; m_LastModify=time(NULL); }
            const string text()const { return m_Text; }

            void setProgress( int value ){m_Progress = value; m_LastModify=time(NULL);}
            int progress()const { return m_Progress;}

            void setID(UID ID){ m_ID = ID; }
            UID id()const { return m_ID;}

            void setLastModify(time_t time){ m_LastModify = time; }
            time_t lastModify() const { return m_LastModify; }

            bool isDeleted(){ return m_IsDeleted; }
            void setDeleted(bool value) { m_IsDeleted = value; }
            
            Task* parent()const{ return m_pParent;}
            int childCount()const;
            Task* child(int ind)const;
            void addTask(Task& newTask);
            void deleteChilds();
            Task* find(UID ID);            
            void removeChild(Task* task);
            void setParent(Task& parent);

            void save(TiXmlElement* parent);
            void load(TiXmlElement* item);
            void calculateCurrentID(int& currentID);
            
            typedef vector<Task*> Vector;

        private:
            void timeToString(time_t time, char*& result);
            time_t stringToTime(const char* strTime);

        private:
            UID             m_ID;
            string          m_Text;
            int             m_Progress;            
            time_t          m_LastModify;
            Task*           m_pParent;
            Vector          m_Childs;
            bool            m_IsDeleted;
    };
}
 
#endif
